package com.zj.aspects;

import com.zj.web.bean.OrderMessageDTO;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Log
@Order(11)
public class orderStatusAspect {
    @Autowired
    private JmsMessageProducter jmsMessageProductr;

    @Pointcut("execution(* com.zj.service.OrdersServiceImpl.updateOrderStatus(..))")
    public void updateOrderStatusPointcut() {
    }

    //通知的类型 before after afterReturning afterThrowing around
    //注册之前
    @AfterReturning(value = "updateOrderStatusPointcut()")
    public void sendOrderStatusMessageToMQ(JoinPoint jp){
        log.info("订单状态更新成功，发送一条要发送给订单状态队列的消息");
        Object[] args = jp.getArgs();
        if (args.length!=3||args[2]==null){
            return;
        }
        Long orderId = (Long) args[0];
        Integer orderStatus = (Integer) args[1];
        String email = (String) args[2];
        // 替换为精简版DTO，不包含product、address、orderdetail字段
        OrderMessageDTO orderMsg = new OrderMessageDTO();
        orderMsg.setOrderId(orderId);
        orderMsg.setOrderStatus(orderStatus);
        orderMsg.setEmail(email);

        log.info("订单状态更新成功，发送精简消息: " + orderMsg);
        jmsMessageProductr.sendOrderStatusMessage(orderMsg); // 确保JmsMessageProducter支持发送该DTO
    }
}
